package com.zl.tby.screen.vo.mountains;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.screen.vo.education.TbyActivitiesStatisticBean;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * 巡山护林统计数据
 * @author changpeng
 * @version 1.0
 * @since 2024/8/20 10:31
 */
@Data
public class TbyMountainsScreenBean {
    /**飞行次数*/
    private Long flightCount;
    /**飞行里程*/
    private BigDecimal flightDistance;
    /**飞行时长*/
    private BigDecimal flightTime;
    /**飞行任务*/
    private String taskCode;
    private Long total;
    private Double percentage;

    public void getFlightTimeHour() {
        if (this.getFlightTime() != null) {
            this.flightTime = ToolsUtil.divide(this.getFlightTime(), new BigDecimal(3600), 1);
        }
    }

    public static TbyMountainsScreenBean initFlightData() {
        TbyMountainsScreenBean entity = new TbyMountainsScreenBean();
        entity.setFlightTime(BigDecimal.ZERO);
        entity.setFlightDistance(BigDecimal.ZERO);
        entity.setFlightCount(0L);
        return entity;
    }

    public void cal() {
        if (total.compareTo(0L) == 0 || flightCount.compareTo(0L) == 0) {
            this.percentage = BigDecimal.ZERO.doubleValue();
            return;
        }
        BigDecimal temp = ToolsUtil.divide(new BigDecimal(this.flightCount), new BigDecimal(this.total), 7);
        temp = temp.multiply(new BigDecimal(100));
        temp = temp.setScale(2, BigDecimal.ROUND_HALF_UP);
        this.percentage = (double) Math.round(temp.doubleValue());
    }

    public static void resetPercentage(List<TbyMountainsScreenBean> dbList) {
        BigDecimal full = new BigDecimal(100);
        // 验证全部大于0
        for (TbyMountainsScreenBean data : dbList) {
            if (data.getPercentage().doubleValue() > 0) {
                return;
            }
        }
        // 避免多次四舍五入不满100的情况
        BigDecimal result = BigDecimal.ZERO;
        if (dbList.size() > 1) {
            for (int i = 0; i < dbList.size() - 1; i++) {
                result = result.add(new BigDecimal(dbList.get(i).getPercentage()));
            }
            dbList.get(dbList.size() - 1).setPercentage(full.subtract(result).doubleValue());
        }
    }
}
